<div class="layuimini-main">

    <div class="layui-form layuimini-form" lay-filter="edit-stu">
        <div class="layui-form-item">
            <label class="layui-form-label required">学号</label>
            <div class="layui-input-block">
                <input type="text" name="sno" lay-verify="required" lay-reqtext="学号不能为空" placeholder="请输入学号" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="username" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required" >性别</label>
            <div class="layui-input-block">
                <select name="sex" lay-verify="required" lay-reqtext="性别不能为空">
                    <option value="">请选择</option>
                    <option value="0" >女</option>
                    <option value="1" >男</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required" >年级</label>
            <div class="layui-input-block">
                <select name="gradeId" id="gradeId" lay-verify="required"></select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">班级</label>
            <div class="layui-input-block">
                <input type="text" name="className" id="className" autocomplete="off"  placeholder="班级不能为空"  readonly  class="layui-input"/>
                <input type="hidden" name="classId" id="classId"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required" >密码</label>
            <div class="layui-input-block">
                <input type="password" name="password" lay-verify="required" lay-reqtext="密码不能为空" placeholder="请输入密码" value="123456" class="layui-input">
                <tip>默认密码：123456</tip>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label required">身份证</label>
            <div class="layui-input-block">
                <input type="text" name="idcard" lay-verify="required" lay-reqtext="身份证不能为空" placeholder="请输入身份证" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">联系方式</label>
            <div class="layui-input-block">
                <input type="phone" name="phone" lay-verify="required" lay-reqtext="联系方式不能为空" placeholder="请输入联系方式" value="10086" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
          <label class="layui-form-label">备注</label>
          <div class="layui-input-block">
              <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
          </div>
          
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认新增</button>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form','axios','layer','selectOrg',"jquery"], function () {

        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            selectOrg = layui.selectOrg,
            axios = layui.axios;
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var DISABLED = 'layui-btn-disabled';
            $(':button').addClass(DISABLED); // 添加样式
            $(':button').attr('disabled', 'disabled');  // 添加属性
            axios({
                method:"post"
                ,url:'/student/add'
                ,data:data.field
            }).then(res=>{
              if(res.code==0){
                layer.msg(res.message,{
                    icon:6,
                    time: 2000 //2秒关闭（如果不配置，默认是3秒）
                },function () {
                    layer.close(parentIndex);
                    layui.table.reload('stuTableId');
                    $(':button').removeClass(DISABLED);
                    $(':button').removeAttr('disabled');
                })
              }else{
                layer.msg(res.message,{
                  icon:5,
                  time:2000,
                  anim:6
                });
                $(':button').removeClass(DISABLED);
                $(':button').removeAttr('disabled');
              }
            })
            .catch(res=>{
              layer.msg("请求错误",{
                  icon:5,
                  time:2000,
                  anim:6
                });
                $(':button').removeClass(DISABLED);
                $(':button').removeAttr('disabled');
            });
            return false;
        });
        //加载年级
        axios({
            method:"get",
            url:"grade/list",
            async:false
        }).then(response=> {
            response_data = response.data.grades
            let options = '<option value="">请选择年级</option>';
            for (let obj of response_data) {
                options += '<option value="' + obj.id + '">' + obj.name + '</option>'
            }
            $('#gradeId').append(options);
            form.render('select');//处理动态赋值
        })
        //加载上级栏目
        selectOrg.render({
            elem:'#className',
            selected:[],
            checked:false,
            url:'org/tree',
            done:function (data) {
                $('#className').val(data[0].name)
                $('#classId').val(data[0].id)
            }
        })
    });
</script>